﻿<?php
	require_once (dirname(__FILE__)."/../config/Config.inc");
	
	$loginadmin = getLoginAdminInfo();
	$m =  getParam('m','get');
	if(empty($loginadmin) && $m != 'login')
	{
		$res['sign'] = '-2';
		echoJSON($res);
		exit;
	}
	
	
	$username = getParam('username','post');
	$password = getParam('password','post');
	$seccode = getParam('seccode','post');	
	$res['sign'] = '0';
	
	$parm = requestArrayParm($_REQUEST);
	
	
	if($m == 'cpUserStatus')
	{
		if(!adminpermission('Y','Y100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			unset($where,$data);
			$where['id'] = $parm['id'];
			$info = _User::getUserInfo($where);
			
			
			
			if($info)
			{
				unset($where,$data);
				$where['id'] = $parm['id'];
				$data['status'] = $parm['status'];
				$upres = _User::setUserInfo($where, $data);
				if($upres)
				{
					saveOpLogInfo('users',$info['id'],'更新用户-状态('.$parm['str'].')');
					
					$res['sign'] = '1';
					$res['msg'] = '操作成功';
				}
				else
				{
					$res['sign'] = '0';
					$res['msg'] = '操作失败';
				}
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '数据错误';
			}
		}
		else
		{
			$res['sign'] = '0';
			$res['msg'] = '数据错误';
		}
		
		echoJSON($res);
	}
	
	if($m == 'chuliTixian')
	{
		if(!adminpermission('Y','Y100003'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']) && !empty($parm['type']))
		{
			$info = _Tixian::getTixianInfo(array('id'=>$parm['id']));
			
			if($info && $info['status'] == '1')
			{
				//成功
				if($parm['type'] == '1')
				{
					if(!empty($parm['ztype']) && !empty($parm['tzsj']))
					{
						unset($data,$where);
						$where['id'] = $info['id'];
						$where['status'] = '1';
						$data['status'] = '2';
						$data['shuoming'] = $parm['shuoming'];
						$data['zztime'] = strtotime($parm['tzsj']);
						
						$number = _Account_number::getAccount_numberInfo(array('id'=>$parm['ztype']));
	
						if($number)
						{
							$data['zztype'] = $number['institution'];
							$data['zznumber'] = $number['number'];
							$data['zzname'] = $number['name'];
							$data['pic'] = $parm['pic'];
							$upres = _Tixian::setTixianInfo($where, $data);
							$parm['str'] = '同意提现';
						}		
						else
						{
							$res['msg'] = '数据异常';
							echoJSON($res);
							exit;
						}
					}
				}
				
				//失败
				if($parm['type'] == '2')
				{
					unset($wheres,$datas);
					$wheres['id'] = $info['id'];
					$wheres['uid'] = $info['uid'];
					$wheres['status'] = '1';
					$datas['status'] = '3';
					$datas['money'] = $info['money'];
					$datas['shuoming'] = $parm['shuoming'];
					$upres = _Tixian::chuliTixian($wheres, $datas);
					$parm['str'] = '拒绝提现';
				}
				
				if($upres)
				{
					if($parm['type'] == '1')
					{
						//发送短信通知
						//发送短信通知
						unset($data,$msg);
						$data['zztype'] = $number['institution'];
						$data['zznumber'] = $number['number'];
						$data['zzname'] = $number['name'];
						$msg['phone'] = $info['phone'];
						$msg['msg'] =  '【伏蒙理财】尊敬的用户您好，您于'.date('Y年m月d日H时',$info['addtime']).'向'.$number['number'].'的'.$number['institution'].'发起的'.$info['money'].'元提现已成功，请关注您的'.$number['institution'].'到账提醒，具体时间以'.$number['institution'].'为准。';
						$jieguo = sendMsg($msg);
					}
					saveOpLogInfo('tixian',$info['id'],'更新提现-状态('.$parm['str'].')');
					
					$res['sign'] = '1';
					$res['msg'] = '处理成功';
					echoJSON($res);
					exit;
				}
				else
				{
					$res['msg'] = '处理失败';
					echoJSON($res);
					exit;
				}
			}
			else
			{
				$res['msg'] = '数据异常';
				echoJSON($res);
				exit;
			}		
			
		}
		else
		{
			$res['msg'] = '数据异常';
			echoJSON($res);
			exit;
		}
		
	}
	
	if($m == 'chuliFanli')
	{
		if(!adminpermission('Y','Y100002'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		//查询返利记录
		if(!empty($parm['id']) && !empty($parm['tzsj']) && !empty($parm['qixian']) && !empty($parm['type']))
		{
			$info = _Fanli::getFanliInfo(array('id'=>$parm['id']));
			if($info)
			{
				if($info['status'] == '1')
				{
					unset($data,$where);
					$where['id'] = $info['id'];
					$where['status'] = '1';
					$data['tzsj'] = strtotime($parm['tzsj']);
					$data['qixian'] = $parm['qixian'];
					$data['shuoming'] = $parm['shuoming'];
					$data['fanli'] = $info['fanli'];
					$data['uid'] = $info['uid'];
					$data['fltime'] = time();
					$data['pic'] = $parm['pic'];
					
					if($data['tzsj'] > $info['addtime'])
					{
						$res['sign'] = '88';
						$res['msg'] = '投资时间应小于申请时间';
						echoJSON($res);
						exit;
					}
					
					
					if($parm['type'] == '1')
					{
						$data['status'] = '2';
						$parm['str'] = '同意返利';
						$upres = _Fanli::chuliFanliInfo($where, $data);
					}
					
					if($parm['type'] == '2')
					{
						$data['status'] = '3';
						$parm['str'] = '拒绝返利';
						$upres = _Fanli::setFanliInfo($where, $data);
					}

					if($upres)
					{
						if($parm['type'] == '1')
						{
							//发送短信通知
							$msg['phone'] = $info['phone'];
							$msg['msg'] =  '【伏蒙理财】尊敬的用户您好，您于'.date('Y年m月d日H时',$info['addtime']).'提交的返利申请已审核成功，请关注您的个人账户信息的余额数。';
							
							sendMsg($msg);
							
							
						}
						
						
						if($parm['type'] == '2')
						{
							//发送短信通知
							$msg['phone'] = $info['phone'];
							$msg['msg'] =  '【伏蒙理财】尊敬的用户您好，您于'.date('Y年m月d日H时',$info['addtime']).'提交的返利申请未审核通过，原因：（'.$data['shuoming'].'）。如有疑问请致电：010-68401564。';
							
							sendMsg($msg);
						}

						saveOpLogInfo('fanli',$info['id'],'更新返利-状态('.$parm['str'].')');
						
						$res['sign'] = '1';
						$res['msg'] = '保存成功';
						echoJSON($res);
						exit;
					}
					else
					{
						$res['msg'] = '保存失败';
						echoJSON($res);
						exit;
					}
				}
				else
				{
					$res['msg'] = '数据异常';
					echoJSON($res);
					exit;
				}
			}
			else
			{
				$res['msg'] = '数据异常';
				echoJSON($res);
				exit;
			}
		}
		else
		{
			$res['msg'] = '数据异常';
			echoJSON($res);
			exit;
		}
	}
	
	if($m == 'zhStatus')
	{
		if(!adminpermission('Z','Z100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			unset($where,$data);
			$where['id'] = $parm['id'];
			$info = _Admin::getAdminInfo($where);
			if($info)
			{
				unset($where,$data);
				$where['id'] = $parm['id'];
				$data['status'] = $parm['status'];
				$upres = _Admin::setAdminInfo($where, $data);
				if($upres)
				{
					saveOpLogInfo('admin',$info['id'],'更新帐号-状态('.$parm['str'].')');
					
					$res['sign'] = '1';
					$res['msg'] = '操作成功';
				}
				else
				{
					$res['sign'] = '0';
					$res['msg'] = '操作失败';
				}
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '数据错误';
			}
		}
		else
		{
			$res['sign'] = '0';
			$res['msg'] = '数据错误';
		}
		
		echoJSON($res);
	}
	
	if($m == 'saveZhanghao')
	{
		if(!adminpermission('Z','Z100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			//更新
			unset($where,$data);
			$where['id'] = $parm['id'];
			
			if(!empty($parm['loginpwd']))
			{
				$data['password'] = md5($parm['loginpwd']);
			}
			$data['loginname'] = $parm['loginname'];
			$data['nickname'] = $parm['name'];
			$data['phone'] = $parm['phone'];
			$data['status'] = $parm['status'];
			$data['permission'] = json_encode($parm['permission']);
			$upres = _Admin::setAdminInfo($where, $data);

			if($upres)
			{
				saveOpLogInfo('admin',$parm['id'],'更新帐号-信息');
				$res['sign'] = '1';
				$res['msg'] = '更新成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '更新失败';
			}
		}
		else
		{
			//添加
			$data['loginname'] = $parm['loginname'];
			$data['password'] = md5($parm['loginpwd']);
			$data['nickname'] = $parm['name'];
			$data['phone'] = $parm['phone'];
			$data['status'] = $parm['status'];
			$data['permission'] = json_encode($parm['permission']);
			$data['regtime'] = time();
			$addres = _Admin::addAdminInfo($data);
			
			if($addres)
			{
				saveOpLogInfo('admin',$addres,'添加帐号-信息');
				$res['sign'] = '1';
				$res['msg'] = '添加成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '添加失败';
			}
		}
		
		echoJSON($res);
		
	}
	
	if($m == 'ggStatus')
	{
		if(!adminpermission('G','G100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			unset($where,$data);
			$where['id'] = $parm['id'];
			$info = _Gonggao::getGonggaoInfo($where);
			if($info)
			{
				unset($where,$data);
				$where['id'] = $parm['id'];
				$data['status'] = $parm['status'];
				$upres = _Gonggao::setGonggaoInfo($where, $data);
				if($upres)
				{
					saveOpLogInfo('gonggao',$info['id'],'更新公告-状态('.$parm['str'].')');
					
					$res['sign'] = '1';
					$res['msg'] = '操作成功';
				}
				else
				{
					$res['sign'] = '0';
					$res['msg'] = '操作失败';
				}
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '数据错误';
			}
		}
		else
		{
			$res['sign'] = '0';
			$res['msg'] = '数据错误';
		}
		
		echoJSON($res);
	}
	
	if($m == 'saveGonggao')
	{
		if(!adminpermission('G','G100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			//更新
			unset($where,$data);
			$where['id'] = $parm['id'];
			$data['sort'] = intval($parm['sort']);
			$data['title'] = $parm['title'];
			$data['content'] = $parm['content'];
			$upres = _Gonggao::setGonggaoInfo($where, $data);
			if($upres)
			{
				saveOpLogInfo('gonggao',$parm['id'],'更新公告-信息');
				$res['sign'] = '1';
				$res['msg'] = '更新成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '更新失败';
			}
		}
		else
		{
			//添加
			$data['sort'] = intval($parm['sort']);
			$data['title'] = $parm['title'];
			$data['content'] = $parm['content'];
			$data['status'] = '1';
			$data['addtime'] = time();
			$addres = _Gonggao::addGonggaoInfo($data);
			
			if($addres)
			{
				saveOpLogInfo('gonggao',$addres,'添加公告-信息');
				$res['sign'] = '1';
				$res['msg'] = '添加成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '添加失败';
			}
		}
		
		echoJSON($res);
	}
	
	if($m == 'cpItemStatus')
	{
		if(!adminpermission('C','C100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			unset($where,$data);
			$where['id'] = $parm['id'];
			$info = _ChanPin::getChanPin_ItemInfo($where);
			if($info)
			{
				unset($where,$data);
				$where['id'] = $parm['id'];
				$data['status'] = $parm['status'];
				$upres = _ChanPin::setChanPin_ItemInfo($where, $data);
				if($upres)
				{
					saveOpLogInfo('chanpin_item',$info['id'],'更新项目-状态('.$parm['str'].')');
					
					$res['sign'] = '1';
					$res['msg'] = '操作成功';
				}
				else
				{
					$res['sign'] = '0';
					$res['msg'] = '操作失败';
				}
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '数据错误';
			}
		}
		else
		{
			$res['sign'] = '0';
			$res['msg'] = '数据错误';
		}
		
		echoJSON($res);
	}
	
	if($m == 'saveCPitem')
	{
		if(!adminpermission('C','C100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			//更新
			unset($where,$data);
			$where['id'] = $parm['id'];
			$data['sort'] = intval($parm['sort']);
			$data['name'] = $parm['name'];
			$data['tupian'] = $parm['tupian'];
			$data['link'] = $parm['link'];
			$data['qixian'] = $parm['qixian'];
			$data['type'] = $parm['type'];
			if($parm['type'] == '1')
			{
				$data['fanli'] = $parm['bili'];
			}
			
			if($parm['type'] == '2')
			{
				$data['fanli'] = $parm['jine'];
			}
			
			$upres = _ChanPin::setChanPin_ItemInfo($where, $data);
			if($upres)
			{
				saveOpLogInfo('chanpin_item',$parm['id'],'更新项目-信息');
				$res['sign'] = '1';
				$res['msg'] = '更新成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '更新失败';
			}
		}
		else
		{
			//添加
			$data['sort'] = intval($parm['sort']);
			$data['name'] = $parm['name'];
			$data['tupian'] = $parm['tupian'];
			$data['link'] = $parm['link'];
			$data['qixian'] = $parm['qixian'];
			$data['type'] = $parm['type'];
			$data['pid'] = $parm['pid'];
			
			
			$data['addtime'] = time();
			if($parm['type'] == '1')
			{
				$data['fanli'] = $parm['bili'];
			}
			
			if($parm['type'] == '2')
			{
				$data['fanli'] = $parm['jine'];
			}
			
			
			$addres = _ChanPin::addChanPin_ItemInfo($data);
			
			if($addres)
			{
				saveOpLogInfo('chanpin_item',$addres,'添加项目-信息');
				$res['sign'] = '1';
				$res['msg'] = '添加成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '添加失败';
			}
		}
		
		echoJSON($res);
	}
	
	if($m == 'cpStatus')
	{
		if(!adminpermission('C','C100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			unset($where,$data);
			$where['id'] = $parm['id'];
			$info = _ChanPin::getChanPinInfo($where);
			if($info)
			{
				unset($where,$data);
				$where['id'] = $parm['id'];
				$data['status'] = $parm['status'];
				$upres = _ChanPin::setChanPinInfo($where, $data);
				if($upres)
				{
					saveOpLogInfo('chanpin',$info['id'],'更新产品-状态('.$parm['str'].')');
					
					$res['sign'] = '1';
					$res['msg'] = '操作成功';
				}
				else
				{
					$res['sign'] = '0';
					$res['msg'] = '操作失败';
				}
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '数据错误';
			}
		}
		else
		{
			$res['sign'] = '0';
			$res['msg'] = '数据错误';
		}
		
		echoJSON($res);
	}
	
	if($m == 'saveChanpin')
	{
		if(!adminpermission('C','C100001'))
		{
			$res['sign'] = '-2';
			echoJSON($res);
			exit;
		}
		
		if(!empty($parm['id']))
		{
			//更新
			unset($where,$data);
			$where['id'] = $parm['id'];
			$data['name'] = $parm['name'];
			$data['title'] = $parm['title'];
			$data['logo'] = $parm['logo'];
			$data['keyw1'] = $parm['keyw1'];
			$data['keyw2'] = $parm['keyw2'];
			$data['keyw3'] = $parm['keyw3'];
			$data['keyw4'] = $parm['keyw4'];
			$data['keyw5'] = $parm['keyw5'];
			$data['shuoming'] = $parm['shuoming'];
			$data['conc'] = $parm['conc'];
			$data['caozuo'] = $parm['caozuo'];
			
			$upres = _ChanPin::setChanPinInfo($where, $data);
			if($upres)
			{
				saveOpLogInfo('chanpin',$parm['id'],'更新产品-信息');
				$res['sign'] = '1';
				$res['msg'] = '更新成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '更新失败';
			}
		}    
		else
		{
			//添加
			$data['name'] = $parm['name'];
			$data['title'] = $parm['title'];
			$data['logo'] = $parm['logo'];
			$data['keyw1'] = $parm['keyw1'];
			$data['keyw2'] = $parm['keyw2'];
			$data['keyw3'] = $parm['keyw3'];
			$data['keyw4'] = $parm['keyw4'];
			$data['keyw5'] = $parm['keyw5'];
			$data['shuoming'] = $parm['shuoming'];
			$data['conc'] = $parm['conc'];
			$data['caozuo'] = $parm['caozuo'];
			$data['addtime'] = time();
			$addres = _ChanPin::addChanPinInfo($data);
			
			if($addres)
			{
				$res['id'] = $addres;
				saveOpLogInfo('chanpin',$addres,'添加产品-信息');
				$res['sign'] = '1';
				$res['msg'] = '添加成功';
			}
			else
			{
				$res['sign'] = '0';
				$res['msg'] = '添加失败';
			}
		}
		
		echoJSON($res);
	}
	
	if($m == 'login')
	{
		$_SESSION['verfyCode'] = '5788';
		if(empty($seccode) || strtolower($seccode) != strtolower($_SESSION['verfyCode']))
		{
			$result['msg'] = '验证码错误!';
		}
		else
		{
			$_SESSION['verfyCode'] = '';
			$loginparm['loginname'] = $username;
			
			$admin = _Admin::getAdminInfo($loginparm);
			
			
			
			
			if(!empty($admin))
			{
				if(md5($password) == $admin['password'])
				{
					$result['sign'] = '1';
					$result['msg'] = '登录成功';
					
					//更新信息
					
					unset($data,$where);
					$where['id'] = $admin['id'];
					$data['lastnums'] = $admin['lastnums'] + 1;
					$data['lastip'] = getClientIP();
					$data['lasttime'] = time();
					
					_Admin::setAdminInfo($where, $data);
				
					$_SESSION['loginadmin'] = $admin;
var_dump($_SESSION);die;
				}
				else
				{
					$result['msg'] = '登录失败,密码错误!';
				}
			}
			else
			{
				$result['msg'] = '登录失败,请检查您的输入!';
			}
		}
		
		echoJSON($result);
	}
